package com.sixteen.ordering_system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sixteen.ordering_system.mapper.SysUserMapper;
import com.sixteen.ordering_system.pojo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sixteen
 * @since 2022-09-16
 */
//@Slf4j
@Controller
public class SysUserController {
    //    依赖注入
    @Autowired
    private SysUserMapper sysUserMapper;

    @RequestMapping("/dologin")
    public String login(SysUser sysUser, HttpSession session) {
        SysUser byUserAndPassword = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("telephone_number", sysUser.getTelephoneNumber()).eq("password", sysUser.getPassword()));
                session.setAttribute("loginUser", byUserAndPassword);
                return "redirect:/index"; //重定向
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "login";
    }

    @PostMapping("/doregister")
    public String doregister(String telephoneNumber,String password){
        SysUser sysUser = sysUserMapper.selectUname(telephoneNumber);
        if (sysUser == null){
            sysUserMapper.saveUser(telephoneNumber,password);
        }
        return "login";
    }
}
